﻿using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;

namespace Core
{
    public class UrlUtil
    {
        /// <summary>
        /// Gets the name of the current page.
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentPageName()
        {
            string path = HttpContext.Current.Request.Url.AbsolutePath;
            System.IO.FileInfo info = new System.IO.FileInfo(path);
            string ret = info.Name;
            return ret;
        }

        /// <summary>
        /// Gets the name of the current page.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns></returns>
        public static string GetCurrentPageName(string path)
        {
            System.IO.FileInfo info = new System.IO.FileInfo(path);
            string ret = info.Name;
            return ret;
        }

        /// <summary>
        /// Gets the name of the previous page.
        /// </summary>
        /// <returns></returns>
        public static string GetPreviousPageName()
        {
            if (HttpContext.Current.Request.UrlReferrer != null)
            {
                return HttpContext.Current.Request.UrlReferrer.Segments[HttpContext.Current.Request.UrlReferrer.Segments.Length - 1];
            }
            return "";
        }

        /// <summary>
        /// Gets the full URL.
        /// </summary>
        /// <param name="relativeUrl">The relative URL.</param>
        /// <returns></returns>
        public static string GetFullUrl(string relativeUrl)
        {
            Page page = (HttpContext.Current.Handler as Page);

            if (page != null)
            {
                string hostName = "http://" + page.Request.ServerVariables["HTTP_HOST"];
                string appPath = page.ResolveUrl(relativeUrl);

                return hostName + appPath;
            }
            return "";
        }

        /// <summary>
        /// Gets the full URL.
        /// </summary>
        /// <returns></returns>
        public static string GetFullUrl()
        {
            Page page = (HttpContext.Current.Handler as Page);

            if (page != null)
            {
                string hostName = "http://" + page.Request.ServerVariables["HTTP_HOST"];
                string appPath = page.ResolveUrl("~");

                string url = hostName + appPath;

                return url.Substring(0, url.Length - 1);
            }
            return "";
        }

        /// <summary>
        /// Determines whether [is local host].
        /// </summary>
        /// <returns>
        /// 	<c>true</c> if [is local host]; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsLocalHost()
        {
            bool isLocalHost = false;

            if (HttpContext.Current.Request.Url.AbsoluteUri.Contains("localhost"))
            {
                isLocalHost = true;
            }

            return isLocalHost;
        }

        /// <summary>
        /// returns "safe" URL, stripping anything outside normal charsets for URL
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <returns></returns>
        public static string SanitizeUrl(string url)
        {
            return Regex.Replace(url, @"[^-A-Za-z0-9+&@#/%?=~_|!:,.;\(\)]", "");
        }

    }
}
